<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles" prefix="tiles"%>
<%@ taglib uri="http://tiles.apache.org/tags-tiles-extras" prefix="tilesx" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
<%@ taglib tagdir="/WEB-INF/tags" prefix="tag"%>
<%@ taglib uri="http://www.springframework.org/security/tags" prefix="security"%>

<script type="text/javascript">
function removeError(itemName)
{
  var itemId = parseIdForJQuery(itemName);
  if ($(itemId + "_errorField").length > 0)
  {
	$("#" + itemName + "_errorField").remove();
	$("#errorCount").val(parseInt(parseInt($("#errorCount").val()) - parseInt(1)));
  }
}

function addError(itemName, errorMessage)
{
  var itemId = parseIdForJQuery(itemName);
  if ($(itemId + "_errorField").length < 1)
  {
	$("#errorsTable").css("display", "block");
	$("#errorCount").val(parseInt(parseInt($("#errorCount").val()) + parseInt(1)));
	$("#errorsTable").html($("#errorsTable").html() + "<tr id='" + itemName + "_errorField' class='errorText'><td>" + errorMessage + "</td>");
	disableButton("saveButton");
  }
  else
  {
	if ($(itemId + "_errorField").css("visibility") == "hidden")
	{
	  $("#errorCount").val(parseInt(parseInt($("#errorCount").val()) + parseInt(1)));
	  $(itemId + "_errorField").css("visibility", "visible").css("display" , "block").html(errorMessage);
      $(itemId).attr("title", errorMessage);
	  disableButton("saveButton");
    }
  }
}

function openSaveChangesDialog()
{
  afterFieldEdit('<spring:message code="field.existing.password"/>', 'string', 'existingPassword', 'false', new Array());
  afterFieldEdit('<spring:message code="field.new.password"/>', 'string', 'newPassword', 'false', new Array());
  afterFieldEdit('<spring:message code="field.confirm.password"/>', 'string', 'newPasswordConfirmation', 'false', new Array());
  setTimeout("checkErrorsForSaveChangesDialog()", 100);
}

function removeSaveMessage()
{
  $('.successfulSave').remove();
}

function validateExistingPassword(itemName)
{
  removeSaveMessage();
  var userNameField = document.getElementById('adminCommand.username');
  var newValueField = document.getElementById(itemName);

  $.ajax({url: 'validation.htm', type: 'POST',
  	      data : {userNameField : userNameField.value,
	    	      newValueField : newValueField.value,
	    	      adminRequestedAction:"validateExistingPassword"}}).success( function (aRes) {    errorMessage = aRes;

																		    		  if (errorMessage != "")
																		    		  {
																		    		    changeClassToError(newValueField);
																		    		    addError(itemName, errorMessage);
																		    		  }
																		    		  else
																		    		  {
																		    		    fieldEdit(itemName);
																		    		    removeError(itemName);
																		    		  }
	    		  													});
}

function validatePassword(itemName)
{
  var newValueField = document.getElementById(itemName);
  var userNameField = document.getElementById("adminCommand.username");

  $.ajax({url: 'validation.htm', type: 'POST',
  	      data : {newValue : newValueField.value,
	    	      userName : userNameField.value,
	    	      adminRequestedAction:"validatePassword"}}).success( function (aRes) {    errorMessage = aRes;
																		    		  if (errorMessage != "")
																		    		  {
																		    		    changeClassToError(newValueField);
																		    		    addError(itemName, errorMessage);
																		    		  }
																		    		  else
																		    		  {
																		    		    fieldEdit(itemName);
																		    		    removeError(itemName);
																		    		  }

																		    		  // Clear the new password confirmation
																		    		  var newPasswordConfirmationField = document.getElementById('newPasswordConfirmation');
																		    		  newPasswordConfirmationField.value = '';
																		    		  fieldEdit('newPasswordConfirmation');
																		    		  removeError('newPasswordConfirmation');
	    		  													});
  }

function validatePasswordConfirm(itemName, passwordItemName)
{
	var newValueField = document.getElementById(itemName);
	var passwordValueField = document.getElementById(passwordItemName);

	$.ajax({url: 'validation.htm', type: 'POST',
  	      data : {newValue : newValueField.value,
	    	      passwordValue : passwordValueField.value,
	    	      adminRequestedAction:"validatePasswordConfirm"}}).success( function (aRes) {    errorMessage = aRes;
																		    		  if (errorMessage != "")
																		    		  {
																		    		    changeClassToError(newValueField);
																		    		    addError(itemName, errorMessage);
																		    		  }
																		    		  else
																		    		  {
																		    		    fieldEdit(itemName);
																		    		    removeError(itemName);
																		    		  }
		  																	    	  checkButtons();
	    		  													});
	}
</script>

<form id="adminForm" name="adminForm" method="post" commandName="adminCommand">

  <form:hidden path="adminCommand.adminRequestedAction" id="adminCommand.adminRequestedAction"/>
  <form:hidden path="adminCommand.successfulSave" id="adminCommand.successfulSave"/>
  <form:hidden path="adminCommand.username" id="adminCommand.username"/>

  <tag:errors name="adminCommand"/>

  <c:if test="${adminCommand.successfulSave}">
  <div class="successfulSave">Successful Save.</div>
  </c:if>

  <table align="center">

    <jsp:include page="/pages/jsp/tiles/admin/errors.jsp"/>

    <security:authorize ifAllGranted="ROLE_EDITABLE">
    <tr>
      <td>
        <table class="adminEdit">
          <tr>
            <th align="center" width="150px"><spring:message code="field.existing.password"/></th>
            <td width="280px">
              <spring:bind path="adminCommand.existingPassword">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="password"
                     id="${status.expression}"
                     name="${status.expression}"
                     value="${status.value}"
                     class="largeTextField"
                     maxlength="30"
                     onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.existing.password"/>', 'string', '${status.expression}', 'false', new Array());validateExistingPassword('${status.expression}');"/>&nbsp;*
              </spring:bind>
            </td>
          </tr>
          <tr>
            <th align="center" width="150px"><spring:message code="field.new.password"/></th>
            <td width="280px">
              <spring:bind path="adminCommand.newPassword">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="password"
                     id="${status.expression}"
                     name="${status.expression}"
                     value="${status.value}"
                     class="largeTextField"
                     maxlength="30"
                     onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.new.password"/>', 'string', '${status.expression}', 'false', new Array());validatePassword('${status.expression}');"/>&nbsp;*
              </spring:bind>
            </td>
          </tr>
          <tr>
            <th align="center" width="150px"><spring:message code="field.confirm.password"/></th>
            <td width="280px">
              <spring:bind path="adminCommand.newPasswordConfirmation">
              <input type="hidden" id="${status.expression}_originalValue" name="${status.expression}_originalValue" value="${status.value}"/>
              <input type="password" id="${status.expression}" name="${status.expression}"
                     value="${status.value}" class="largeTextField" maxlength="30"
                     onchange="fieldEdit('${status.expression}');afterFieldEdit('<spring:message code="field.confirm.password"/>', 'string', '${status.expression}', 'false', new Array());validatePasswordConfirm('newPasswordConfirmation', 'newPassword');"/>&nbsp;*
              </spring:bind>
            </td>
          </tr>
        </table>
      </td>
    </tr>
    <tr>
      <td>
        <table align="center" style="margin-top:10px;">
          <tr>
            <td>
              <a id="saveButton" class="button_inputDisabled"><span><spring:message code="button.save"/></span></a></td>
          </tr>
        </table>
      </td>
    </tr>
    </security:authorize>
    <security:authorize ifNotGranted="ROLE_EDITABLE">
    <tr><td>Not authorized to change password.</td></tr>
    </security:authorize>

    <jsp:include page="/pages/jsp/tiles/admin/hoverConfirmation.jsp" />
  </table>
</form>